package com.ly.hotel.view.message;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.msgevent.UpdateMessageView;
import com.ly.hotel.util.DateUtils;
import com.ly.hotel.view.common.BaseActivity;
import com.ly.hotel.view.common.HotelMassage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import de.greenrobot.event.EventBus;


public class MessageActivity extends BaseActivity {

    private ListView msgList;
    private TextView msgContent;
    private TextView msgCount;
    private List<HotelMassage> hotelMassages;
    private MyAdapter myAdapter;
    private MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        app = (MyApplication) getApplicationContext();
        hotelMassages = app.getRtMsgList();
        msgList = findViewById(R.id.msg_List);
        msgContent = findViewById(R.id.msg_content);
        msgCount = findViewById(R.id.unread_num);
        setListener();
        setAdapter();
    }

    private void setListener(){
        msgList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                myAdapter.setsPos(position);
                setContent(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        msgList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                myAdapter.setsPos(position);
                setContent(position);
                final int pos = position;
                msgList.post(new Runnable() {
                    @Override
                    public void run() {
                        msgList.requestFocusFromTouch();
                        msgList.setSelection(pos);
                    }
                });
            }
        });
    }

    private void setContent(int pos){
        msgContent.setText(Html.fromHtml(hotelMassages.get(pos).getContent()));
        if(!hotelMassages.get(pos).getRead()) {
            hotelMassages.get(pos).setRead(true);
        }
        setAdapter();
    }

    public void onEventMainThread(UpdateMessageView event) {
        setAdapter();
        msgList.post(new Runnable() {
            @Override
            public void run() {
                msgList.setSelection(0);
            }
        });
    }

    private void setAdapter() {
        int num = 0;
        for (HotelMassage hm : hotelMassages){
            if (!hm.getRead()){
                num++;
            }
        }
        msgCount.setText(getResources().getString(R.string.msg_unread) + num);
        if(myAdapter == null) {
            myAdapter = new MyAdapter(this);
            msgList.setAdapter(myAdapter);
        }else {
            myAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(new UpdateMessageView());
    }

    class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int sPos = -1;

        public MyAdapter(Context c) {
            mInflater = LayoutInflater.from(c);
        }

        public void setsPos(int pos){
            this.sPos = pos;
        }

        @Override
        public int getCount() {
            return hotelMassages.size();
        }

        @Override
        public Object getItem(int postion) {
            return hotelMassages.get(postion);
        }

        @Override
        public long getItemId(int postion) {
            return postion;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {
            ViewHolder holder;
            if (contentView == null) {
                holder = new ViewHolder();
                contentView = mInflater.inflate(R.layout.message_item, null);
                holder.mState = contentView.findViewById(R.id.msg_item_img);
                holder.mTitle = contentView.findViewById(R.id.msg_item_time);
                holder.mContent = contentView.findViewById(R.id.msg_item_content);
                contentView.setTag(holder);
            } else {
                holder = (ViewHolder) contentView.getTag();
            }

            if (position == sPos){
                holder.mTitle.setTextColor(Color.parseColor("#000000"));
                holder.mContent.setTextColor(Color.parseColor("#000000"));
            }else {
                holder.mTitle.setTextColor(Color.parseColor("#ffffff"));
                holder.mContent.setTextColor(Color.parseColor("#ffffff"));
            }

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dateServer = null;
            String str = hotelMassages.get(position).getTime();

            if(hotelMassages.get(position).getRead()){
                holder.mState.setImageResource(R.drawable.msg_read);
            }else {
                holder.mState.setImageResource(R.drawable.msg_unread);
            }

            try {
                dateServer = format.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(dateServer != null) {
                String myDate = DateUtils.getDateString(dateServer, getResources());
                holder.mTitle.setText(myDate + "     ");
                holder.mTitle.append(DateUtils.getTimeString(dateServer, getResources()));
            }else {
                holder.mTitle.setText(str);
            }
            holder.mContent.setText(Html.fromHtml(hotelMassages.get(position).getContent()));
            return contentView;
        }

        class ViewHolder {
            ImageView mState;
            TextView mTitle;
            TextView mContent;
        }
    }
}
